from django.contrib.auth.models import User
from django.db import models


class UserProfile(models.Model):
    user = models.OneToOneField(User, unique=True)
    email_verify = models.IntegerField(null=True, blank=True)
    sex = models.IntegerField(null=True, blank=True)
    birthday = models.DateField(null=True, blank=True)
    last_login_time = models.DateTimeField(null=True, blank=True)
    create_time = models.DateTimeField(null=True, blank=True)
    update_time = models.DateTimeField(null=True, blank=True)
    status = models.IntegerField(null=True, blank=True)
    avatar = models.ImageField(upload_to='photo', null=True, blank=True)

    def __str__(self):
        return self.user.username + " | " + str(self.create_time)

    class Meta:
        ordering = ['user']


class Tag(models.Model):
    tag_name = models.CharField(max_length=20, blank=False)

    def __str__(self):
        return self.tag_name


class ArticleCategory(models.Model):
    type = models.CharField(max_length=20, blank=False)

    def __str__(self):
        return self.type


class Article(models.Model):
    title = models.CharField(max_length=200)
    content = models.TextField()
    view_count = models.IntegerField(null=True, blank=True)
    create_time = models.DateTimeField(null=True, blank=True)
    update_time = models.DateTimeField(null=True, blank=True)
    status = models.IntegerField(null=True, blank=True)
    user = models.ForeignKey(UserProfile, blank=False)
    category = models.ForeignKey(ArticleCategory, blank=False)

    def __str__(self):
        return self.title + " " + str(self.create_time)

    class Meta:
        ordering = ['create_time']


class TagTelation(models.Model):
    article = models.ForeignKey(Article, blank=False)
    tag = models.ForeignKey(Tag, blank=False)

    def __str__(self):
        return self.article.title + "--" + self.tag.tag_name
